package restPROC;

import java.sql.SQLException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.ws.rs.Consumes;
import javax.ws.rs.FormParam;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;

import utilClasses.ListaLinija;
import dao.daoKonekcija;
import dao.daoLinija;
import dao.daoPermisije;



@Path("/linije") 
public class Linije {
	
	@POST
	@Path("/unesi")
	@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
	@Produces("plain/text")
	public String unesiLiniju(@FormParam("linija") String linija,@FormParam("cijena") double cijena,@FormParam("idMape") int idMape,@FormParam("prevoznik") String prevoznik,@FormParam("saobracaj") String saobracaj,@Context HttpServletRequest req)
	{
		HttpSession sess = req.getSession(true);
		String reqkorisnik=sess.getAttribute("korisnik").toString();
		System.out.println(reqkorisnik);
		try {
			boolean jelima=daoPermisije.provjeri(reqkorisnik, "modlinije");
			System.out.println(jelima);
			if(!jelima) return null;
		} catch (SQLException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		try {
			
			daoLinija.unesiLiniju(linija, cijena, idMape, prevoznik, saobracaj);
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return "false";
		}
		return "true";
	}
	
	@POST
	@Path("/listalinija")
	@Produces({"application/xml"})
	public ListaLinija getLinije()
	{
		ListaLinija rez = new ListaLinija();
		try {
			
			rez=daoLinija.dajLinije();
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return rez;
		
	}
	

	@POST
	@Path("/brisanje")
	@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
	@Produces("text/plain")
	public String deleteLiniju(@FormParam("id") String id,@Context HttpServletRequest req)
	{
		HttpSession sess = req.getSession(true);
		String reqkorisnik=sess.getAttribute("korisnik").toString();
		System.out.println(reqkorisnik);
		try {
			boolean jelima=daoPermisije.provjeri(reqkorisnik, "modlinije");
			System.out.println(jelima);
			if(!jelima) return null;
		} catch (SQLException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		try {
			
			System.out.print(id);
			daoLinija.brisiLiniju(Integer.parseInt(id));
			
			
			return "true";
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return "false";
	}
	
	@GET
	@Path("/filtriranje")
	@Produces({"application/xml"})
	public ListaLinija filterLinija(@QueryParam("filter") String filter)
	{
		ListaLinija rez = new ListaLinija();
		
		try {
			
			rez=daoLinija.dajLinijeLike(filter+"%");

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return rez;
	}
	
	@GET
	@Path("/sortiranje")
	@Produces({"application/xml"})
	public ListaLinija Sortiranje()
	{
		ListaLinija rez = new ListaLinija();
		
		try {
			
			rez=daoLinija.dajSortiraneLinije();

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return rez;
	}
}